package com.iscc.fake.controller;

import com.google.gson.Gson;
import com.iscc.fake.domain.Address;
import com.iscc.fake.domain.Carrt;
import com.iscc.fake.domain.User;
import com.iscc.fake.service.AddressService;
import com.iscc.fake.service.CartService;
import com.iscc.fake.service.Impl.AddressServiceImpl;
import com.iscc.fake.service.Impl.CartServiceImpl;
import com.iscc.fake.service.Impl.ProductServiceImpl;
import com.iscc.fake.service.ProductService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.channels.NonWritableChannelException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet({"/add_cart", "/select_cart", "/del_cart","/deleteAll_cart","/select_sum","/select_logo"})
public class CartServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();//URI目标为 /fake/register.do；
        String action = uri.substring(uri.lastIndexOf("/") + 1);
        PrintWriter out = response.getWriter();
        //转化为Json
        Gson gson = new Gson();

        HttpSession session=request.getSession();
        /*User user = new User(1);

        session.setAttribute("user",user);*/


        CartService cartService= new CartServiceImpl();
        Map<String, Object> addressMap = new HashMap<>();
        if ("add_cart".equals(action)){
            int pno = Integer.parseInt(request.getParameter("pno"));
            int count = Integer.parseInt(request.getParameter("count"));
            double price = Double.parseDouble(request.getParameter("price"));

            //从session中拿到
            int uno = ((User) session.getAttribute("user")).getUno();

            Carrt carrt=new Carrt(uno,pno,price,count);
            int add = cartService.addCart(carrt);
            if (add != -1) {
                addressMap.put("code", 200);
                addressMap.put("msg", "购物车物品添加成功");
            } else {
                addressMap.put("code", 201);
                addressMap.put("msg", "购物车物品添加失败");
            }

        }else if ("del_cart".equals(action)){
            int cartno = Integer.parseInt(request.getParameter("cartno"));
            int del = cartService.delCart(cartno);
            if (del != -1) {
                addressMap.put("code", 200);
                addressMap.put("msg", "删除成功");
            } else {
                addressMap.put("code", 201);
                addressMap.put("msg", "添加失败");
            }


        }else if ("select_cart".equals(action)){
            List<Carrt> carSerciveList = cartService.selectCart();
            if (carSerciveList.size() > 0) {
                addressMap.put("code", 200);
                addressMap.put("result", carSerciveList);
            } else {
                addressMap.put("code", 201);
            }



        }else if ("deleteAll_cart".equals(action)){
            int deAll=cartService.delAllCart();
            if (deAll != -1) {
                addressMap.put("code", 200);
                addressMap.put("msg", "购物车物品删除成功");
            } else {
                addressMap.put("code", 201);
                addressMap.put("msg", "购物车物品删除失败");
            }

        }else if ("select_sum".equals(action)){
            double selectSum=cartService.selectSum();
            if (selectSum != -1) {
                addressMap.put("code", 200);
                addressMap.put("msg", "查询总价格成功");
                addressMap.put("result", selectSum);
            } else {
                addressMap.put("code", 201);
                addressMap.put("msg", "查询总价格失败");
            }

        }else if ("select_logo".equals(action)){
            int uno = Integer.parseInt(request.getParameter("uno"));
            Carrt carrt=cartService.seletlogo(uno);
            addressMap.put("result", carrt);
        }
        String str = gson.toJson(addressMap);
        out.print(str);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
    }
}
